<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/26
 * Time: 下午3:03
 */

namespace App\Busi\Modules\Auth;


use App\Busi\Modules\BaseBusi;
use App\Busi\Traits\CaptchaTrait;
use App\Exceptions\ErrorCode\UserError;
use App\Exceptions\UserException;
use App\Http\Requests\ResetPwdRo;
use App\Models\User;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class ResetPwdByPhoneBusi extends BaseBusi
{
    use CaptchaTrait;
    private  $captcha_key = "captcha:forgetpwd:";
    public function handle(ResetPwdRo $ro){
        $this->checkCaptcha($this->captcha_key.$ro->phone, $ro->captcha);
        if($ro->password !=$ro->confirm_password){
            UserException::touch(UserError::PWD_CONFIRM_ERROR);
        }
        try{
            $user = User::wherePhone($ro->phone)->firstOrFail();
            $user->password = md5($ro->confirm_password);
            $user->saveOrFail();
            $data = array(
                'uid'=>$user['uid'],
                'name' => $user['name'],
                'phone' => $user['phone'],
                'avatar' => $user['avatar'],


            );
            return $data;
        }catch (ModelNotFoundException $e) {
            UserException::touch(UserError::USER_NOT_EXIST);
        }

    }
}